/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.utrans.ejb;

import com.utrans.entity.Ruta;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Predicate;
import javax.persistence.Query;

/**
 *
 * @author Gamboa
 */
@Stateless
public class RutaFacade extends AbstractFacade<Ruta> {
    @PersistenceContext(unitName = "Utrans-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public RutaFacade() {
        super(Ruta.class);
    }
    
    
    public List<Ruta> rutasDeEstacion(int[] range, int codeEstacion){
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Object> cq = cb.createQuery();
        Root<Ruta> from = cq.from(Ruta.class);
        CriteriaQuery<Object> select = cq.select(from);
        
        Predicate predicate = cb.equal(from.get("estacionList").get("codigoEstacion"), codeEstacion);
               
        select.where(predicate);
        select.orderBy(cb.asc(from.get("nombreRuta")));
        Query q = getEntityManager().createQuery(select);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        System.out.println("Codigo de Ruta " +codeEstacion);
        return q.getResultList();
        
    }
    
    public int countRutasDeEstacion(int codEstacion) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Object> cq = cb.createQuery();
        Root<Ruta> from = cq.from(Ruta.class);
        CriteriaQuery<Object> select = cq.select(from);
        
        Predicate predicate = cb.equal(from.get("estacionList").get("codigoEstacion"), codEstacion);
               
        select.where(predicate);        
        Query q = getEntityManager().createQuery(select);        
        return q.getResultList().size();
    }
    
    public List<Ruta> findRangeByNameCode(int[] range, String nameCode) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Object> cq = cb.createQuery();
        Root from = cq.from(Ruta.class);
        CriteriaQuery<Object> select = cq.select(from);
        
        Expression<String> name = cb.upper(cb.literal((String) "%"+nameCode+"%"));
        Predicate predicate = cb.like(cb.upper(from.get("nombreRuta")), name);
        try{                    
            int code=Integer.parseInt(nameCode);           
            Predicate predicate2 = cb.equal(from.get("codigoRuta"), code);
            cq.where(cb.or(predicate, predicate2));
        }catch(NumberFormatException nfe){
            cq.where(predicate);
        }
        select.orderBy(cb.asc(from.get("nombreRuta")));
        Query q = getEntityManager().createQuery(select);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }
    
    public int countByNameCode(String nameCode) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Object> cq = cb.createQuery();
        Root from = cq.from(Ruta.class);
        CriteriaQuery<Object> select = cq.select(from);
        
        Expression<String> name = cb.upper(cb.literal((String) "%"+nameCode+"%"));
        Predicate predicate = cb.like(cb.upper(from.get("nombreRuta")), name);
        try{                    
            int code=Integer.parseInt(nameCode);           
            Predicate predicate2 = cb.equal(from.get("codigoRuta"), code);
            cq.where(cb.or(predicate, predicate2));
        }catch(NumberFormatException nfe){
            cq.where(predicate);
        }
        Query q = getEntityManager().createQuery(select);
        return q.getResultList().size();
    }
    
}
